﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.Misc;
using System.Collections.ObjectModel;
using System.Collections;

namespace CSharp.Libs.WPF.UserControls
{
    class DataLogBasicControlViewModel : ViewModelBase
    {
        #region declarations
        private DataLogSQLite _dlSQLite = null;

        private ObservableCollection<string> _ocLevels = new ObservableCollection<string>();
        private ObservableCollection<string> _ocCategories = new ObservableCollection<string>();
        private ObservableCollection<DataLogItem> _ocResult = new ObservableCollection<DataLogItem>();

        private string _strSelectedFromTime = NO_SELECTION;
        private string _strSelectedToTime = NO_SELECTION;
        private string _strSelectedLevel = NO_SELECTION;
        private string _strSelectedCategory = NO_SELECTION;

        private const string NO_SELECTION = "<No Selection>";
        #endregion



        #region constructor / disposer
        public DataLogBasicControlViewModel()
        {
            _dlSQLite = new DataLogSQLite();
            _dlSQLite.InitDb();
            _getLevels();
            _getCategories();

            _getResult();
        }

        protected override void OnDispose()
        {
            _dlSQLite.Dispose();
            _dlSQLite = null;

            base.OnDispose();
        }
        #endregion



        #region private functions
        private void _getLevels()
        {
            ArrayList alLevels = _dlSQLite.GetLevels();

            ObservableCollection<string> oc = new ObservableCollection<string>();
            oc.Add(NO_SELECTION);

            if (alLevels != null &&
                alLevels.Count > 0)
            {
                foreach (string strLevel in alLevels)
                {
                    oc.Add(strLevel);
                }
            }

            Levels = oc;
        }

        private void _getCategories()
        {
            ArrayList alCategories = _dlSQLite.GetCategories();

            ObservableCollection<string> oc = new ObservableCollection<string>();
            oc.Add(NO_SELECTION);

            if (alCategories != null &&
                alCategories.Count > 0)
            {
                foreach (string strCategory in alCategories)
                {
                    oc.Add(strCategory);
                }
            }

            Categories = oc;
        }

        private void _getResult()
        {
            //clear result
            if (_ocResult.Count > 0) { _ocResult.Clear(); }

            ArrayList alResult = _dlSQLite.QueryLogItems(
                strFromTime: "",
                strToTime: "",
                strLevel: _strSelectedLevel != NO_SELECTION ? _strSelectedLevel : "",
                strCategory: _strSelectedCategory != NO_SELECTION ? _strSelectedCategory : "",
                strTags: "",
                strMessageFilter: "");

            if (alResult != null &&
                alResult.Count > 0)
            {
                foreach (DataLogItem dli in alResult)
                {
                    _ocResult.Add(dli);
                }
            }

            OnPropertyChanged("Result");
        }
        #endregion



        #region interface
        #region properties
        public ObservableCollection<string> Levels { get { return _ocLevels; } set { _ocLevels = value; OnPropertyChanged("Levels"); } }
        public ObservableCollection<string> Categories { get { return _ocCategories; } set { _ocCategories = value; OnPropertyChanged("Categories"); } }
        public ObservableCollection<DataLogItem> Result { get { return _ocResult; } }

        public string SelectedFromTime { get { return _strSelectedFromTime; } set { _strSelectedFromTime = value; OnPropertyChanged("SelectedFromTime"); } }
        public string SelectedToTime { get { return _strSelectedToTime; } set { _strSelectedToTime = value; OnPropertyChanged("SelectedToTime"); } }
        public string SelectedLevel { get { return _strSelectedLevel; } set { _strSelectedLevel = value; OnPropertyChanged("SelectedLevel"); _getResult(); } }
        public string SelectedCategory { get { return _strSelectedCategory; } set { _strSelectedCategory = value; OnPropertyChanged("SelectedCategory"); _getResult(); } }
        #endregion
        #endregion
    }
}
